

package com.situ.mvc.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mvc.entity.Banji;
import com.situ.mvc.entity.PageBean;
import com.situ.mvc.entity.Student;
import com.situ.mvc.entity.StudentSearchCondition;
import com.situ.mvc.service.IBanjiService;
import com.situ.mvc.service.IStudentService;
import com.situ.mvc.service.impl.BanjiServiceImpl;
import com.situ.mvc.service.impl.StudentServiceImpl;

@Controller
@RequestMapping(value="/student")
public class StudentController {
	private IStudentService studentService = new StudentServiceImpl();
	private IBanjiService banjiService = new BanjiServiceImpl();
	
	//添加页面可选择班级
	//查出所有班级信息，放到域对象，转发student_add,jsp页面
	@RequestMapping(value="/getAddPage")
	public String getAddPage(Model model) {
		List<Banji> list = banjiService.list();
		model.addAttribute("list", list);
		return "student/student_add";
	}
	
	//ajax,使添加名字不重复	
	@RequestMapping(value="/checkName")
	public void checkName(String name,HttpServletRequest request, HttpServletResponse response) throws IOException{
		boolean isExist =  studentService.checkName(name);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"isExist\": "+isExist+"}");
	}
	
	//删除根据ID
	@RequestMapping(value="/deleteById")
	@ResponseBody
	private Boolean deleteById(Integer id) {
		boolean deresult = studentService.deleteById(id);
		System.out.println(deresult ? "成功" : "失败");
		//3、重定向到列表界面
		//return "redirect:/student/pageList.action";
		return deresult;
	}	

	//批量删除
	@RequestMapping(value="/deleteAll")
	@ResponseBody
	public Boolean deleteAll(String[] selectIds) {
		System.out.println(Arrays.toString(selectIds));
		boolean result = studentService.deleteAll(selectIds);
		System.out.println(result ? "成功" : "失败");
		return result;
	}
	
	//分页
	@RequestMapping(value="/pageList")
	private String pageList(Integer pageNo,Integer pageSize,String name,Integer age,String gender ,Model model) {
		//接受请求参数，封装成对象
		if (pageNo == null) {
			pageNo = 1;
		}
		if (pageSize == null) {
			pageSize = 3;
		}
		//所有要查询的条件封装
		StudentSearchCondition searchCondition = new StudentSearchCondition(pageNo, pageSize, name, age, gender);
		//封装成pageBean，调用业务逻辑
		PageBean<Map<String, Object>> pageBean = studentService.getPageBean(searchCondition);
		System.out.println(pageBean);
		//控制界面跳转
		model.addAttribute("pageBean", pageBean); //要展示分页的信息
		model.addAttribute("searchCondition", searchCondition);
		return "student/student_list";
	}
	
	//真正修改
	@RequestMapping(value="/update")
	@ResponseBody
	private Boolean update(Integer id,String name,Integer age,String gender,Integer banjiId){
	//1、得到浏览器输入的参数信息
		Student student = new Student(id, name, age, gender, banjiId);
		//Student student = new Student(id, name, age, gender);
		//2、调用sevice
		boolean result = studentService.update(student);
		System.out.println(result ? "成功" : "失败");
		//3、重定向
		//return "redirect:/student/pageList.action";
		return result;
	}
	
	//欲修改
	@RequestMapping(value="/toUpdate")
	private String toUpdate(Integer id ,Model model) {
		//根据ID查出要修改的学生对象
		Student student =  studentService.findById(id);//根据查找指定学生对象，直接使用
		//显示班级列表
		List<Banji> list = banjiService.list();
		model.addAttribute("list", list);
		//转发到student_update.jsp页面
		model.addAttribute("student", student);
		return "student/student_update";//转发
	}
	
	//Layer、ajax弹窗	，第二步到这里，拿到班级名字的列表，在insert页面展示
	@RequestMapping(value="getInsertPage")
	public String getInsertPage(Model model) {
		List<Banji> list = banjiService.list();
		model.addAttribute("list", list);
		return "student/student_insert";
	}
	
	//添加,从form表单过来
	@RequestMapping(value="/insert")
	@ResponseBody
	private Boolean insert(String name,Integer age,String gender,Integer banjiId) {
		//1、得到浏览器输入的参数信息
		Student student = new Student(name, age, gender, banjiId); //页面展示班级
		//2、调用sevice
		boolean result = studentService.insert(student);
		System.out.println(result ? "成功" : "失败");
		//3、重定向
		//return "redirect:/student/pageList.action";//重定向
		return  result;//重定向
	}
	
	//展示
	/*@RequestMapping(value="/list")
	private String list(String name,Model model) {
		//List<Student> list = studentService.list();//service方法得到列表
		//调用service
		List<Student> list = new ArrayList<>();
		if (name == null || name.equals("")) {//查找所有
		list = studentService.list(); //
			} else {
		//展示
		for (Student student : list) {  //遍历
			System.out.println(student);
		}
		//list放到req里面
		model.addAttribute("list", list);
		//转发页面展示
		}
		return "student_list.action";
	}*/
}
